package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.util.contact.ContactBean;
import com.shd.edanpei.util.contact.ContactsAdapter;
import com.shd.edanpei.util.contact.DeviceContactsImportTools;
import com.shd.edanpei.presenter.contract.ContactsContract;
import com.shd.edanpei.presenter.ContactsPresenter;
import com.shd.edanpei.util.DensityUtil;
import com.shd.edanpei.util.LayoutUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/***
 * 通讯录
 */
public class ContactsActivity extends BaseActivity<ContactsPresenter>
        implements ContactsContract.ContactsIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.iv_headline_right)
    ImageView ivRight;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;

    @BindView(R.id.ll_contact)
    View listLayout;
    @BindView(R.id.rlv_contacts)
    RecyclerView rlvContacts;

    @BindView(R.id.ll_search)
    View serachLayout;
    @BindView(R.id.rlv_search)
    RecyclerView rlvSearch;
    @BindView(R.id.tv_search_hint)
    TextView tvSearchHint;

    private DeviceContactsImportTools importTools;// 设备通讯录导入工具
    private ContactsAdapter mAdapter;// 联系人列表adapter
    private ContactsAdapter serachAdapter;// 搜索结果列表adapter
    private Bundle bundle = new Bundle();

    @Override
    protected int setLayout() {
        return R.layout.activity_contracts;
    }

    @Override
    protected ContactsPresenter bindPresenter() {
        return new ContactsPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        //标题
        headline.setText("通讯录");
        //标题右方iocn：新建联系人
        ivRight.setImageResource(R.drawable.ic_add_contract);
        ivRight.setVisibility(View.VISIBLE);
        //联系人列表
        mAdapter = new ContactsAdapter(this);
        rlvContacts.setLayoutManager(new LinearLayoutManager(this));
        rlvContacts.setAdapter(mAdapter);
        //搜索结果列表
        serachAdapter = new ContactsAdapter(this);
        rlvSearch.setLayoutManager(new LinearLayoutManager(this));
        rlvSearch.setAdapter(serachAdapter);
        //获取联系人列表
        mPresenter.getContacts();
    }

    @Override
    protected void initListener() {
        etSearch.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    etSearch.setFocusable(true);
                    etSearch.setFocusableInTouchMode(true);
                    etSearch.requestFocus();
                    etSearch.findFocus();
                    tvCancel.setVisibility(View.VISIBLE);
                }
                return false;
            }
        });

        etSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (etSearch.getText().toString().isEmpty()) {
                        return true;
                    } else {
                        mPresenter.toSearch(etSearch.getText().toString());

                    }
                }
                return false;
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                bundle.clear();
                bundle.putParcelable(Constant.DATA, mAdapter.getItem(position));
                startActivity(UserDetailsActivity.class, bundle);
            }
        });

    }

    @OnClick(R.id.iv_headline_back)
    void close() {
        finish();
    }

    @OnClick(R.id.iv_headline_right)
    void addContact() {
        startActivity(NewCustomerActivity.class);
    }

    /**
     * 导入手机通讯录
     */
    @OnClick(R.id.ll_lead_contracts)
    void leadContracts() {
        if (importTools == null) {
            importTools = new DeviceContactsImportTools(this);
        }
        if (importTools != null) {
            importTools.startLead(new DeviceContactsImportTools.Callback() {
                @Override
                public void start() {
                    showLoading("正在导入");
                }

                @Override
                public void done(List<ContactBean> list) {
                    mPresenter.leadContracts(list);
                    stopLoading();
                }
            });
        }
    }


    /**
     * 点击取消搜索
     *
     * @return
     */
    @OnClick(R.id.tv_cancel)
    void onCancel() {
        etSearch.setText("");
        etSearch.setFocusable(false);
        DensityUtil.hintImm(this);
        listLayout.setVisibility(View.VISIBLE);
        serachLayout.setVisibility(View.GONE);
        tvCancel.setVisibility(View.GONE);
        tvSearchHint.setVisibility(View.GONE);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public ContactsAdapter getAdapter() {
        return mAdapter;
    }

    @Override
    public void setSearchData(List<ContactBean> list) {
        if (list.size() != 0) {
            tvSearchHint.setVisibility(View.VISIBLE);
            serachAdapter.setNewData(list);
        } else {
            tvSearchHint.setVisibility(View.GONE);
            serachAdapter.setNewData(null);
            serachAdapter.setEmptyView(LayoutUtils.getNullLayout(this, R.drawable.pic_noperson, "查无此人，请重新输入"));
        }
        serachLayout.setVisibility(View.VISIBLE);
        listLayout.setVisibility(View.GONE);
        DensityUtil.hintImm(this);
    }


}
